var opeType = true;
var jzjs = {
    setBtnStatus: function () {
        var row = $('#grid_jzjs').datagrid('getSelected');
/*
        if (row && row.zt !=1) {   //被选择的应付信用证
            $("#btn_jzjs_add").show();
            $("#btn_jzjs_tj").show();
            $("#btn_jzjs_del").show();
        } else {  //未被选择的应付票据
            $("#btn_jzjs_add").show();
            $("#btn_jzjs_tj").hide();
            $("#btn_jzjs_del").show();
        }

*/
        if (row && row.id && row.zt) {
            if (row.zt == 0) {
                $("#btn_jzjs_add").show();
                $("#btn_jzjs_tj").hide();
                $("#btn_jzjs_del").hide();
            } else if (row.zt ==1) {
                $("#btn_jzjs_add").show();
                $("#btn_jzjs_tj").hide();
                $("#btn_jzjs_del").hide();
            } else {
                $("#btn_jzjs_add").show();
                $("#btn_jzjs_tj").show();
                $("#btn_jzjs_del").show();
            }
        } else {
            $("#btn_jzjs_add").show();
            $("#btn_jzjs_tj").hide();
            $("#btn_jzjs_del").hide();
        }

    }
}

$(function () {
    $("#jzjsgsId").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });

    //表格初始加载
    $('#grid_jzjs').datagrid({
        url: $.webPath() + "/zjgl/jzjs/jzjsList",
        toolbar: "#toolbar_jzjs",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        checked: true,
        checkOnSelect: true,
        selectOnCheck: false,
        rownumbers: true,
        striped: true,
        showFooter: true,
        columns:[
            [
                {field: 'nbgs_jc', title: '运营商', width: 100, align: 'center'},
                {field: 'cght_htbm', title: '合同编码', width: 120, align: 'center'},
                {field: 'cght_hsze', title: '合同金额(元)', width: 100, align: 'center'},
                {field: 'kh_qc', title: '客户', width: 150, align: 'center'},
                {field: 'fklx', title: '货款类型', width: 70, align: 'center',
                    formatter: function (value) {
                        if (value == "0")
                            return "现汇";
                        if (value == "1")
                            return "承兑";
                    }
                },
                {field: 'khfksj', title: '客户付款时间', width: 70, align: 'center'},
                {field: "sk_bm", title: "收款单", width: 100, align: 'right'/*, formatter: function (value, row, index) {
                    if (value) {
                        return '<a href="#" id="bzj_bm_sfkbm" class="ye2-linkbutton" style="color:red" sfkBm="' + row["sk_bm"] + '" >' + value + '</a>';
                    }
                }*/
                },
                {field: 'zt', title: '货款状态', width: 70, align: 'center',
                    formatter: function (value) {
                        if (value == "0")
                            return "已锁定";
                        if (value == "1")
                            return "已解锁";
                        if (value == "2")
                            return "已保存";

                    }},
                {field: 'je', title: '货款金额(元)', width: 100, align: 'right',
                    formatter: function (value) {
                        if (!value)
                            return "无";
                        else
                            return formatJe2(value, ",");
                    }
                },{field: 'mxts', title: '免息天数', width: 70, align: 'center'},
                {field: 'txl', title:  '贴息率（%）', width: 85, align: 'center'},
                {field: 'jsts', title: '计算天数', width: 70, align: 'center'},
                {field: 'ye', title:   '余额(元)', width: 100, align: 'right',
                    formatter: function (value) {
                        if (!value)
                            return "无";
                        else
                            return formatJe2(value, ",");
                    }
                }

            ]
        ]
       /* columns: [
            [
                {field: 'je', title: '货款金额(元)', width: 100, align: 'right',
                    formatter: function (value) {
                        if (!value)
                            return "无";
                        else
                            return formatJe2(value, ",");
                    }
                },
                {field: 'ye', title: '货款余额(元)', width: 100, align: 'right',
                    formatter: function (value) {
                        if (!value)
                            return "无";
                        else
                            return formatJe2(value, ",");
                    }
                },
                {field: 'txl', title: '月贴息率（%）', width: 85, align: 'center'},
                {field: 'endDate', title: '到期日期', width: 70, align: 'center'},
                {field: 'tx', title: '贴息金额(元)', width: 80, align: 'center'},
                {field: 'txye', title: '贴息余金(元)', width: 80, align: 'center'},
                {field: 'mxts', title: '免息天数', width: 70, align: 'center'},
                {field: 'jsts', title: '计算天数', width: 70, align: 'center'}
            ]
        ]*/,
        onLoadSuccess:
            function () {
     /*      $(this).datagrid('selectRow', 0);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                jzjs.setBtnStatus();
            $(".ye2-linkbutton").linkbutton({plain: true}).unbind("click").bind("click", function () {
                $.window({
                    title: '保证金收款明细',
                    width: 860,
                    height: 480,
                    cache: false,
                    maximizable: true,
                    url: $.webPath() + '/zjgl/bzj/bzj_sk_detail?sfkBm=' + $(this).attr("sfkBm"),
                    modal: true,
                    buttons: [],
                    onMaximize: function () {
                        $(this).find("#grid_jzjs").edatagrid("resize");
                    },
                    onRestore: function () {
                        $(this).find("#grid_jzjs").edatagrid("resize");
                    }
                });
            });*/
        },
        onSelect: function (rowIndex, rowData) {
            jzjs.setBtnStatus();
        },
        onCheck: function () {
            jzjs.setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //新增按钮
    $("#btn_jzjs_add").click(function () {
        jzjs_add('居中结算支付');
    });

    //查询按钮
    $("#btn_jzjs_search").click(function () {
        jzjs_search();
    });

    $("#btn_jzjs_tj").click(function () {
        jzjs_tj();
    });

    $("#btn_jzjs_del").click(function () {
        jzjs_destroyItem();
    });

    /*    //合同解锁
        $("#btn_hthk_js").click(function () {
            hthk_js();
        });*/

});
//新增保证金
function jzjs_add(title) {
    opeType = true;
    var href = $.webPath() + "/zjgl/jzjs/jzjs_content";
    var actionUrl = $.webPath() + "/zjgl/jzjs/add_jzjs";
    jzjs_showDialog(title, href, actionUrl);
}

// 新增窗口
function jzjs_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 400,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_jzjs').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_jzjs').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//查询保证金
function jzjs_search() {

    var gsMc = $("#jzjsgsId").combotree('getText') == null ? null : $("#jzjsgsId").combotree('getText');
    var gsId = $("#jzjsgsId").combotree('getValue') == null ? null : $("#jzjsgsId").combotree('getValue');
    var hth = $("#jzjs_hth").val();
   var khMc = $("#jzjs_kh").val();
    $('#grid_jzjs').datagrid('load',
        {
            gsId: gsId,
            gsMc: gsMc,
            htbm: hth,
            khMc: khMc
        }
    );
}


function jzjs_tj() {
    var row = $('#grid_jzjs').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否提交该货款？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/jzjs/tj_jzjs",
                    dataType: 'json',
                    type: 'POST',
                    data: {jzjsid: row.id},
                    success: function (data) {
                        if (data.statusCode == 200) {
                            $('#grid_jzjs').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要提交的货款！", "warning");
    }
}


function jzjs_destroyItem() {
    var row = $('#grid_jzjs').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该单据？', function (r) {
            if (r) {
                var index = $('#grid_jzjs').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/zjgl/jzjs/delete_jzjs",
                    dataType: 'json',
                    type: 'POST',
                    data: {jzjsid:row.id},
                    beforeSend: function () {
                        $("#layout_jzjs").mask();
                    },
                    success:function(data){
                        $("#layout_jzjs").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_jzjs').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });

            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的单据！", "warning");
    }
}


/*
function hthk_js() {
    var row = $('#grid_hthk').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否解锁该货款？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/hthk/js_hthk",
                    dataType: 'json',
                    type: 'POST',
                    data: {hthkid: row.id},
                    success: function (data) {
                        if (data.statusCode == 200) {
                            $('#grid_hthk').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要解锁的货款！", "warning");
    }
}
*/
